﻿using Convertor.Interface;
using System;
using System.Windows;
using Wpf.CommonTools.SystemExtension;
using Wpf.CommonTools.WebExtension;

namespace Convertor.Plugin.UrlEncode
{
    public class Plugin : PluginBase
    {

        #region Static Func

        static string[] LanguagePaths = new string[] { @"Convertor.Plugin.UrlEncode;component\Assets\Lang\StringResource.zh.xaml", @"Convertor.Plugin.UrlEncode;component\Assets\Lang\StringResource.en.xaml" };

        static Plugin()
        {
            foreach (string path in LanguagePaths)
            {
                ResourceDictionary resourceDictionary = new ResourceDictionary();
                resourceDictionary.Source = new Uri(path, UriKind.Relative);
                Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
            }
        }

        #endregion

        #region IPluginInfo 成员

        public override string Name
        {
            get { return UILanguage.FindResource("PluginType_Web") + " - " + UILanguage.FindResource("Plugin_UrlEncode_Name"); }
        }


        public override string Description
        {
            get { return UILanguage.FindResource("Plugin_UrlEncode_Desc"); }
        }





        #endregion

        #region IConvertParamters 成员

        public override IConvertParamters Output(IConvertParamters content)
        {
            if (content == null || content.OutputContent == null)
            {
                if (Log != null) { Log(this, UILanguage.FindResource("Plugin_Common_Input_Output_Null"), LogTypes.Error); }
                content.StopConvert = true;
                return content;
            }

            if (Log != null) { Log(this, config.ToolTip, LogTypes.Normal); }

            string convertedStr;
            switch (config.EncodeType)
            {
                default:
                case EncodeTypes.UrlEncode:
                    convertedStr = System.Web.HttpUtility.UrlEncode(content.OutputContent.ToString());
                    break;
                case EncodeTypes.UrlDecode:
                    convertedStr = System.Web.HttpUtility.UrlDecode(content.OutputContent.ToString());
                    break;
                case EncodeTypes.HtmlEncode:
                    convertedStr = System.Web.HttpUtility.HtmlEncode(content.OutputContent.ToString());
                    break;
                case EncodeTypes.HtmlDecode:
                    convertedStr = System.Web.HttpUtility.HtmlDecode(content.OutputContent.ToString());
                    break;
                case EncodeTypes.Escape:
                    convertedStr = content.OutputContent.ToString().ToEscape();
                    break;
                case EncodeTypes.UnEscape:
                    convertedStr = content.OutputContent.ToString().ToUnEscape();
                    break;
                case EncodeTypes.ToUnicode:
                    convertedStr = content.OutputContent.ToString().ToUnicode();
                    break;
                case EncodeTypes.FromUnicode:
                    convertedStr = content.OutputContent.ToString().FromUnicode();
                    break;
            }

            if (Log != null) { Log(this, string.Format(UILanguage.FindResource("Plugin_UrlEncode_ConvertSuccess"), EncodeTypes.FromUnicode), LogTypes.Normal | LogTypes.HidePluginName); }

            return new ConvertParamters(content.LastInputContent, convertedStr);
        }

        #endregion

        #region IPluginConfigForm 成员

        public override PluginConfigForm ConfigForm
        {
            get { return new ConfigForm(); }
        }

        #endregion

        #region IPluginConfig 成员

        private ConfigContent config;

        public override object Config
        {
            get { return config; }
            set { config = (value as ConfigContent) ?? new ConfigContent(); }
        }

        #endregion

    }
}
